/*     */ package com.asteros.common.util.http;
/*     */ 
/*     */ import java.io.BufferedWriter;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.io.OutputStreamWriter;
/*     */ import java.io.PrintStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.io.StringWriter;
/*     */ import java.io.Writer;
/*     */ import java.net.HttpURLConnection;
/*     */ import java.net.URL;
/*     */ 
/*     */ public final class Client
/*     */ {
/*     */   public static final String METHOD_GET = "GET";
/*     */   public static final String METHOD_POST = "POST";
/*     */   public static final String METHOD_PUT = "PUT";
/*     */   public static final String METHOD_DELETE = "DELETE";
/*     */   public static final String UTF8 = "UTF8";
/*     */   public static final String TEXT_PLAIN_CONTENT_TYPE = "text/plain";
/*     */   public static final String XML_CONTENT_TYPE_UTF8 = "text/xml;charset=UTF-8";
/*     */   public static final String CONTENT_TYPE_HEADER = "Content-Type";
/*     */   public static final String LOCATION_HEADER = "Location";
/*     */   private static final String SOAP_ACTION_HEADER = "soapaction";
/*     */   private static final String SOAP_ACTION_HEADER_VALUE = "";
/*  39 */   private String url = null;
/*     */ 
/*     */   public Client() {
/*     */   }
/*     */ 
/*     */   public Client(String url) {
/*  45 */     setUrl(url);
/*     */   }
/*     */   public String getUrl() {
/*  48 */     return this.url;
/*     */   }
/*     */   public void setUrl(String url) {
/*  51 */     this.url = url;
/*     */   }
/*     */ 
/*     */   public String call() {
/*  55 */     return call("");
/*     */   }
/*     */ 
/*     */   public byte[] callData(byte[] buf)
/*     */   {
/*  60 */     byte[] response = null;
/*     */     try
/*     */     {
/*  63 */       HttpURLConnection connect = (HttpURLConnection)new URL(getUrl()).openConnection();
/*  64 */       if (!connect.getDoOutput()) {
/*  65 */         connect.setDoOutput(true);
/*     */       }
/*  67 */       connect.setRequestMethod("POST");
/*  68 */       connect.setUseCaches(false);
/*     */ 
/*  72 */       OutputStream output = connect.getOutputStream();
/*     */ 
/*  74 */       output.write(buf);
/*  75 */       output.flush();
/*  76 */       output.close();
/*     */ 
/*  78 */       if (!connect.getDoInput()) {
/*  79 */         connect.setDoInput(true);
/*     */       }
/*  81 */       InputStream input = connect.getInputStream();
/*  82 */       response = readFully(input);
/*     */ 
/*  84 */       System.out.println("resp.size=" + response.length);
/*     */ 
/*  86 */       input.close();
/*  87 */       connect.disconnect();
/*     */     }
/*     */     catch (Exception e) {
/*  90 */       StringWriter w = new StringWriter();
/*  91 */       e.printStackTrace(new PrintWriter(w));
/*  92 */       System.err.println(w.toString());
/*  93 */       return null;
/*     */     }
/*     */ 
/*  96 */     return response;
/*     */   }
/*     */ 
/*     */   public String call(String query)
/*     */   {
/* 101 */     String response = null;
/* 102 */     log("request>>\n", query, "\n");
/*     */     try {
/* 104 */       HttpURLConnection connect = (HttpURLConnection)new URL(getUrl()).openConnection();
/* 105 */       if (!connect.getDoOutput()) {
/* 106 */         connect.setDoOutput(true);
/*     */       }
/* 108 */       connect.setRequestMethod("POST");
/* 109 */       connect.setUseCaches(false);
/* 110 */       connect.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
/* 111 */       connect.setRequestProperty("soapaction", "");
/*     */ 
/* 123 */       OutputStream output = connect.getOutputStream();
/* 124 */       Writer out = new BufferedWriter(new OutputStreamWriter(output, "UTF8"));
/*     */ 
/* 126 */       out.write(query);
/* 127 */       out.flush();
/* 128 */       out.close();
/*     */ 
/* 133 */       if (!connect.getDoInput()) {
/* 134 */         connect.setDoInput(true);
/*     */       }
/* 136 */       InputStream input = connect.getInputStream();
/* 137 */       byte[] buf = readFully(input);
/*     */ 
/* 139 */       input.close();
/* 140 */       connect.disconnect();
/*     */ 
/* 142 */       response = new String(buf, "UTF8");
/*     */     } catch (Exception e) {
/* 144 */       StringWriter w = new StringWriter();
/* 145 */       e.printStackTrace(new PrintWriter(w));
/* 146 */       System.err.println(w.toString());
/* 147 */       return null;
/*     */     }
/* 149 */     log("response>>\n", response, "\n");
/* 150 */     return response;
/*     */   }
/*     */ 
/*     */   public HttpResponse call(String data, String method)
/*     */   {
/* 155 */     return call(data, method, "UTF8", "text/xml;charset=UTF-8");
/*     */   }
/*     */ 
/*     */   public HttpResponse call(String data, String method, String enc, String contentType)
/*     */   {
/* 160 */     HttpResponse response = null;
/* 161 */     log("Client.call.data>>\n", data, "\n");
/*     */     try {
/* 163 */       HttpURLConnection connect = (HttpURLConnection)new URL(getUrl()).openConnection();
/* 164 */       if ((data != null) && (data.length() > 0) && 
/* 165 */         (!connect.getDoOutput())) connect.setDoOutput(true);
/*     */ 
/* 167 */       connect.setRequestMethod(method);
/* 168 */       connect.setUseCaches(false);
/* 169 */       connect.setRequestProperty("Content-Type", contentType);
/* 170 */       connect.setRequestProperty("soapaction", "");
/*     */ 
/* 182 */       if ((data != null) && (data.length() > 0)) {
/* 183 */         OutputStream output = connect.getOutputStream();
/* 184 */         Writer out = new BufferedWriter(new OutputStreamWriter(output, enc));
/*     */ 
/* 186 */         out.write(data);
/* 187 */         out.flush();
/* 188 */         out.close();
/*     */       }
/*     */ 
/* 194 */       if (!connect.getDoInput()) {
/* 195 */         connect.setDoInput(true);
/*     */       }
/* 197 */       InputStream input = connect.getInputStream();
/* 198 */       byte[] buf = readFully(input);
/*     */ 
/* 200 */       input.close();
/*     */ 
/* 202 */       int responseCode = connect.getResponseCode();
/* 203 */       String responseMessage = connect.getResponseMessage();
/* 204 */       String responseContentType = connect.getHeaderField("Content-Type");
/* 205 */       String responseLocation = connect.getHeaderField("Location");
/*     */ 
/* 212 */       connect.disconnect();
/*     */ 
/* 214 */       response = new HttpResponse(responseCode, responseMessage, new String(buf, enc));
/* 215 */       response.putHeaderValue("Content-Type", responseContentType);
/* 216 */       response.putHeaderValue("Location", responseLocation);
/*     */     } catch (Exception e) {
/* 218 */       StringWriter w = new StringWriter();
/* 219 */       e.printStackTrace(new PrintWriter(w));
/* 220 */       System.err.println(w.toString());
/* 221 */       return null;
/*     */     }
/*     */ 
/* 224 */     return response;
/*     */   }
/*     */ 
/*     */   private void log(String title, String query, String endTitle)
/*     */   {
/*     */   }
/*     */ 
/*     */   private static byte[] readFully(InputStream is)
/*     */     throws IOException
/*     */   {
/* 237 */     byte[] buf = new byte[1024];
/* 238 */     int num = 0;
/*     */ 
/* 240 */     ByteArrayOutputStream bout = new ByteArrayOutputStream();
/* 241 */     while ((num = is.read(buf)) != -1) {
/* 242 */       bout.write(buf, 0, num);
/*     */     }
/*     */ 
/* 245 */     return bout.toByteArray();
/*     */   }
/*     */ }
